﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace MsPacmanController
{
    public static class Extensions
    {

        /// <summary>
        /// Copies the Bitmap to a managed array
        /// </summary>
        /// <param name="bitmap">the source</param>
        /// <returns></returns>
        public static int[] ToArray(this Bitmap bitmap)
        {
            int size = bitmap.Width * bitmap.Height;            
            int[] result = new int[size];
            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
            IntPtr ptr = bitmapData.Scan0;
            Marshal.Copy(ptr, result, 0, size);
            bitmap.UnlockBits(bitmapData);
            return result;
        }

    }
}
